// 统一配置模块
// 引入dotenv加载环境变量
const path = require('path');

// 确定当前环境
const NODE_ENV = process.env.NODE_ENV || 'development';

// 根据当前环境加载对应的.env文件
const envFile = process.env.NODE_ENV === 'test' ? '.env.test' : '.env';
require('dotenv').config({ path: path.resolve(__dirname, `../${envFile}`) });


// 基础配置
const baseConfig = {
  env: NODE_ENV,
  isDev: NODE_ENV === 'development',
  isTest: NODE_ENV === 'test',
  isProd: NODE_ENV === 'production',
  port: process.env.PORT || 3000,
};

// 数据库配置
const dbConfig = {
  // 通用数据库配置
  common: {
    dialect: 'mysql',
    logging: baseConfig.isDev ? console.log : false,
    dialectOptions: {
      charset: 'utf8mb4',
      supportBigNumbers: true,
      bigNumberStrings: true
    },
    define: {
      timestamps: true,
      paranoid: true,
      underscored: true,
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
    },
  },
  
  // 开发/生产环境数据库配置
  production: {
    host: process.env.DB_HOST || 'localhost',
    port: process.env.DB_PORT || 3306,
    username: process.env.DB_USER || 'root',
    password: process.env.DB_PASS || '',
    database: process.env.DB_NAME || 'flower_db',
    ...baseConfig.common
  },
  
  // 测试环境数据库配置
  test: {
    host: process.env.DB_HOST_TEST || 'localhost',
    port: process.env.DB_PORT_TEST || 3306,
    username: process.env.DB_USER_TEST || 'root',
    password: process.env.DB_PASS_TEST || '',
    database: process.env.DB_NAME_TEST || 'flower_db_test',
    ...baseConfig.common
  },
  
  // 开发环境数据库配置
  development: {
    host: process.env.DB_HOST || 'localhost',
    port: process.env.DB_PORT || 3306,
    username: process.env.DB_USER || 'root',
    password: process.env.DB_PASS || '',
    database: process.env.DB_NAME || 'flower_db',
    ...baseConfig.common
  }
};

// 完整配置对象
const config = {
  ...baseConfig,
  db: dbConfig[NODE_ENV] || dbConfig.development,
  // 其他配置项可以在这里添加
  jwt: {
    secret: process.env.JWT_SECRET || 'your_jwt_secret',
    expiresIn: process.env.JWT_EXPIRES_IN || '24h'
  }
};

module.exports = config;